package com.example.k.littletag.activity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.example.k.littletag.R;
import com.example.k.littletag.component.RevealBackgroundView;

import java.util.Calendar;


/**
 * Created by Kong on 2015/9/29.
 */
public class PickTime extends Activity {
    public static final String ARG_REVEAL_START_LOCATION = "reveal_start_location";
    public static final String TIME_PICKED = "time_picked";

    private RevealBackgroundView revealView;
    private RelativeLayout rlayoutTime;
    private DatePicker datePicker;
    private TimePicker timePicker;
    private TextView timeDay;
    private TextView timeHour;
    private TextView timeSpace;
    private TextView timeTip;
    private Calendar calendar;

    private ImageButton resultButton;

    private int year;
    private int month;
    private int day;
    private int hour;
    private int minute;
    private int[] time;
    private int[] location;


    public static void start(int[] location, Activity startingActivity) {
        Intent intent = new Intent(startingActivity, PickTime.class);
        intent.putExtra(ARG_REVEAL_START_LOCATION, location);
        startingActivity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pick_time_activity);

        findViewById(R.id.time_result).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });


        initView();
        initPickView();
        initRevealView(savedInstanceState);

        resultButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();

                intent.putExtra(TIME_PICKED, time);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }


    @Override
    public void onBackPressed() {
        setResult(RESULT_CANCELED, null);
        super.onBackPressed();
    }

    private void initView() {
        time = new int[6];
        location = new int[2];
        calendar = Calendar.getInstance();
        resultButton = (ImageButton) findViewById(R.id.time_result);
        time[0] = year = calendar.get(Calendar.YEAR);
        time[1] = month = calendar.get(Calendar.MONTH);
        time[2] = day = calendar.get(Calendar.DAY_OF_MONTH);
        time[3] = hour = calendar.get(Calendar.HOUR_OF_DAY);
        time[4] = minute = calendar.get(Calendar.MINUTE);
        time[5] = 0;
        rlayoutTime = (RelativeLayout) findViewById(R.id.rLayout_time);
        revealView = (RevealBackgroundView) findViewById(R.id.revealView);
        datePicker = (DatePicker) findViewById(R.id.dataPicker);
        timePicker = (TimePicker) findViewById(R.id.timePicker);
        timeDay = (TextView) findViewById(R.id.time_day);
        timeDay.setText(year + " - " + (month + 1) + " - " + day);
        timeHour = (TextView) findViewById(R.id.time_hour);
        timeHour.setText(hour + " : " + minute);
        timeSpace = (TextView) findViewById(R.id.space);
        timeTip = (TextView) findViewById(R.id.time_tip);
    }


    private void initPickView() {
        datePicker.init(0, 0, 0, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                timeDay.setText(year + " - " + (monthOfYear+1) + " - " + dayOfMonth);
                time[0] = year;
                time[1] = monthOfYear+1;
                time[2] = dayOfMonth;
            }
        });
        timePicker.setIs24HourView(true);
        timePicker.setCurrentHour(hour);
        timePicker.setCurrentMinute(minute);
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                String s;
                if (minute < 10) {
                    s = "0" + minute;
                }
                s = "" + minute;
                timeHour.setText(hourOfDay + " : " + s);
                time[3] = hourOfDay;
                time[4] = minute;
            }
        });
    }

    private void initRevealView(Bundle savedInstanceState) {
        revealView.setfillPaintColor(getResources().getColor(R.color.reveal_red));
        revealView.setOnStateChangeListener(new RevealBackgroundView.OnStateChangeListener() {
            @Override
            public void onStateChange(int state) {
                if (state == RevealBackgroundView.STATE_FINISHED) {
                    //getApplication().setTheme(R.style.AppTheme_Base);
                    rlayoutTime.setBackgroundColor(getResources().getColor(R.color.reveal_red));
                    revealView.setVisibility(View.GONE);
                    datePicker.setVisibility(View.VISIBLE);
                    timePicker.setVisibility(View.VISIBLE);
                    timeDay.setVisibility(View.VISIBLE);
                    timeHour.setVisibility(View.VISIBLE);
                    timeSpace.setVisibility(View.VISIBLE);
                    timeTip.setVisibility(View.VISIBLE);

                    showAnimation();
                }
            }
        });

        if (savedInstanceState == null) {
            final int[] startLocation = getIntent().getIntArrayExtra(ARG_REVEAL_START_LOCATION);
            Log.d("locationInWindows2", startLocation[0] + "    " + startLocation[1] + "");

            revealView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    revealView.getViewTreeObserver().removeOnPreDrawListener(this);
                    revealView.startFromLocation(startLocation);
                    return true;
                }
            });
        } else {
            revealView.setToFinishedFrame();
        }
    }

    private void showAnimation() {
        ObjectAnimator alpha_time = ObjectAnimator.ofFloat(timePicker, "alpha", 0f, 1f);
        ObjectAnimator alpha_date = ObjectAnimator.ofFloat(datePicker, "alpha", 0f, 1f);
        ObjectAnimator alpha_day = ObjectAnimator.ofFloat(timeDay, "alpha", 0f, 1f);
        ObjectAnimator alpha_hour = ObjectAnimator.ofFloat(timeHour, "alpha", 0f, 1f);
        ObjectAnimator alpha_tip = ObjectAnimator.ofFloat(timeTip, "alpha", 0f, 1f);


        AnimatorSet animset = new AnimatorSet();
        animset.play(alpha_date).with(alpha_time).with(alpha_day).with(alpha_hour).with(alpha_tip);
        animset.setDuration(300);
        animset.start();
    }

}
